package cn.com.unionlife.hy.system.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.com.unionlife.hy.core.controller.BaseController;
import cn.com.unionlife.hy.core.exception.BusinessException;
import cn.com.unionlife.hy.system.service.MenuQryService;
import cn.com.unionlife.hy.system.service.OrgQryService;
import cn.com.unionlife.hy.system.service.UserQryService;
import cn.com.unionlife.hy.system.service.UserTranService;

@Controller("userController")
public class UserController extends BaseController {
	
	@Resource(name ="userQryService")
	private UserQryService userQryService;
	@Resource(name = "userTranService")
	private UserTranService userTranService;
	
	@Resource(name = "orgQryService")
	private OrgQryService orgQryService;
	
	@Resource(name = "menuQryService")
	private MenuQryService menuQryService;

	@RequestMapping(value = "/system/user/list")
	public String listUser(@RequestParam(value="orgId" ,required=false)String orgId , Model model) throws BusinessException{
		if(StringUtils.isNotBlank(orgId)){
			model.addAttribute("users", userQryService.findUserByOrg(orgId));
		}
		return "/system/user/list";
	}
	
	@RequestMapping(value = "/system/user/{cmd}/{id}")
	public String enableUser(@PathVariable("id")String id ,@PathVariable("cmd")String cmd, Model model) throws BusinessException{
		String msg = "";
		if("enabled".equals(cmd)){
			userTranService.enableUser(id, true);
			msg = "启用成功";
		}else if("disabled".equals(cmd)){
			userTranService.enableUser(id, false);
			msg = "停用成功";
		}else{
			msg = "非法操作";
		}
		return ajaxDone(200, msg, null, model);
	}
	
	
	@RequestMapping(value = "/system/user/authorize" , method= RequestMethod.GET)
	public String goAuthorize(HttpServletRequest request , @RequestParam(value = "userId" , required = true)String userId , Model model) throws BusinessException{
		model.addAttribute("orgs", orgQryService.getUserAuthorizedOrg(getLoginedUser(request).getUm_user_id() , null , true , true));
		model.addAttribute("hasOrgs", orgQryService.getUserAuthorizedOrg(userId  , null , false, false));
		return "/system/user/authorize";
	}
	@RequestMapping(value = "/system/user/authorize" , method= RequestMethod.POST)
	public String doAuthorize(@RequestParam(value = "userId" , required = true)String userId , @RequestParam(required = false ,value ="orgIds")String[] orgIds, Model model) throws BusinessException{
		userTranService.authorizeOrg(userId, orgIds);
		return ajaxDone(200, "分配组织成功", null, model);
	}
	@RequestMapping(value = "/system/user/assignmenu" , method = RequestMethod.GET)
	public String goAssignMenu(HttpServletRequest request , @RequestParam(value = "userId" , required = true)String userId , Model model) throws BusinessException{
		model.addAttribute("menus", menuQryService.getUserMenu(getLoginedUser(request).getUm_user_id()));
		model.addAttribute("hasMenus", menuQryService.getUserMenu(userId));
		return "/system/user/assignMenu";
	}
	@RequestMapping(value = "/system/user/assignmenu" ,method=RequestMethod.POST)
	public String doAssignMenu(@RequestParam(value = "userId" , required = true)String userId , @RequestParam(value = "menuIds" , required = false)Long[] menuIds , Model model) throws BusinessException{
		userTranService.assignMenu(userId, menuIds);
		return ajaxDone(200, "分配组织成功", null, model);
	}
}
